// 校验手机号码
function checkMobile() {
    var mobile = $("#mobile").val();
    if (mobile == null || mobile == "") {
        $("#mobileError").text("请输入正确的手机号！");
        $("#mobileError").show();
        return false;
    }
    var mobileReg = /^1\d{10}$/;
    if (!mobileReg.test(mobile)) {
        $("#mobileError").text("请输入正确的手机号！");
        $("#mobileError").show();
        return false;
    }
    $("#mobileError").hide();
    return true;
}

// 校验邮箱
function checkEmail(){
    var email = $("#email").val();
    if(email == null || email == ""){
        $("#emailError").text("请输入正确的邮箱！");
        $("#emailError").show();
        return false;
    }
    var reg = /^\w+[@]\w{2,5}([.]\w{2,3}){1,3}$/i;
    if(!reg.test(email)){
        $("#emailError").text("请输入正确的邮箱！");
        $("#emailError").show();
        return false;
    }
    $("#emailError").hide();
    return true;
}

// 发送验证码
function sendCheckCode(type){
    // 校验手机号码
    if(type == 1 && !checkMobile()){
        return false;
    }
    // 校验邮箱
    if(type == 2 && !checkEmail()){
        return false;
    }
    var elementObj = type == 1 ? $("#checkCodeBtnMobile") : $("#checkCodeBtnEmail");
    var timer = setInterval(beginTime,1000);
    var countdown = 60;
    function beginTime(){
        elementObj.text(countdown + "秒后获取");
        if (countdown == 1) {
            elementObj.text("获取验证码");
            //进度百分百后，清除掉定时器
            clearInterval(timer);
            return false;
        } else {
            if (countdown == 60) {
                // 请求地址
                var url = type == 1 ? '/user-center-oauth/public/sms/restCaptcha/send' : "/user-center-oauth/public/email/restCaptcha/send";
                var account = type == 1 ? $("#mobile").val() : $("#email").val();
                var data = type == 1 ? {phone: account} : {email: account};
                $.ajax({
                    url: url,
                    type: "post",
                    data: JSON.stringify(data),
                    headers: {'Content-Type': 'application/json'},
                    dataType: "json",
                    success: function (data) {
                        if (data != "") {
                            if (data.code == 200) {
                                alert("验证码发送成功");
                            } else {
                                alert(data.msg);
                                elementObj.text("获取验证码");
                                //进度百分百后，清除掉定时器
                                clearInterval(timer);
                            }
                        }
                    }
                });
            }
            countdown--;
        }
    }
}

function validateCheckCodeMobile(){
    var checkCode = $("#checkCodeMobile").val();
    if(checkCode == ""){
        $("#checkCodeErrorMobile").text("请输入验证码");
        $("#checkCodeErrorMobile").show();
        return false;
    }
    $("#checkCodeErrorMobile").hide();
    return true;
}

function validateCheckCodeEmail(){
    var checkCode = $("#checkCodeEmail").val();
    if(checkCode == ""){
        $("#checkCodeErrorEmail").text("请输入验证码");
        $("#checkCodeErrorEmail").show();
        return false;
    }
    $("#checkCodeErrorEmail").hide();
    return true;
}

function registerNext(type){
    // 校验手机号码
    if(type == 1 && !checkMobile()){
        return false;
    }
    // 校验邮箱
    if(type == 2 && !checkEmail()){
        return false;
    }
    if(type == 1 && !validateCheckCodeMobile()){
        return false;
    }
    if(type == 2 && !validateCheckCodeEmail()){
        return false;
    }
    var url = type == 1 ? "/user-center-oauth/public/sms/captcha/check" : "/user-center-oauth/public/email/captcha/check";
    var data = type == 1 ? {phone: $("#mobile").val(), captcha: $("#checkCodeMobile").val()} : {email: $("#email").val(), captcha: $("#checkCodeEmail").val()};
    var account = type == 1 ? $("#mobile").val() : $("#email").val();
    $.ajax({
        url: url,
        type: "post",
        data: JSON.stringify(data),
        headers: {'Content-Type': 'application/json'},
        dataType: "json",
        success: function (data) {
            if (data != "" && data.code == 200) {
                window.location.href="forgetpwdNext.html?type=" + type + "&account=" + account + "&resetPasswdToken=" + data.data.resetPasswdToken;
            }else{
                alert(data.msg);
            }
        }
    });
}

// 校验密码
function checkPassword(){
    var password = $("#password").val();
    if(password == null || password == ""){
        $("#passwordError").text("密码长度在8到32之间！");
        $("#passwordError").show();
        return false;
    }
    var reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&.])[A-Za-z\d$@$!%*?&]{8,32}/;
    if(!reg.test(password)){
        $("#passwordError").text("密码提示：输入密码应当是8-32位字符，包含数字、小写、大写、特殊字符");
        $("#passwordError").show();
        return false;
    }
    $("#passwordError").hide();
    return true;
}

// 校验确认密码
function checkPasswordSure(){
    var password = $("#password").val();
    var passwordSure = $("#passwordSure").val();
    if(passwordSure == null || passwordSure == ""){
        $("#passwordErrorSure").text("请输入确认密码！");
        $("#passwordErrorSure").show();
        return false;
    }
    if(passwordSure != password){
        $("#passwordErrorSure").text("两次密码输入不一致！");
        $("#passwordErrorSure").show();
        return false;
    }
    $("#passwordErrorSure").hide();
    return true;
}

function resetPassword() {
    var type = getUrlParam("type");
    var account = getUrlParam("account");
    var resetPasswdToken = getUrlParam("resetPasswdToken");
    if(!checkPassword()){
        return false;
    }
    if(!checkPasswordSure()){
        return false;
    }
    var password = $("#password").val();
    var passwordEn = encrypt(password, "e0da368a8fa3d9b3f691e82957ec5b75").toUpperCase();
    var url = type == 1 ? "/user-center-oauth/public/sms/passwd/update" : "/user-center-oauth/public/email/passwd/update";
    var data = type == 1 ? {phone: account, passwd: passwordEn, confirmPasswd: passwordEn, resetPasswdToken: resetPasswdToken}
                    : {email: account, passwd: passwordEn, confirmPasswd: passwordEn, resetPasswdToken: resetPasswdToken};
    console.log(data);
    $.ajax({
        url: url,
        type: "post",
        data: JSON.stringify(data),
        headers: {'Content-Type': 'application/json'},
        dataType: "json",
        success: function (data) {
            if (data != "" && data.code == 200) {
                alert("设置密码成功");
                window.location.href="login.html";
            }else{
                alert(data.msg);
            }
        }
    });
}